package deatax;

import deatax.display.Model;
import deatax.display.ModelLoader;
import deatax.display.Texture;
import deatax.display.gui.ResourcesPanel;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.JPanel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author pson
 */
public class Terrain {

    /**
     * name of this terrain
     */
    private String name;
    private int moveCost;
    private int waterCost;
    private ResourcesPanel popup;
    private int score;
    /**
     * The resource produced by this type of terrain
     */
    private ArrayList<ResourceAmount> bonuses;
    //private Resource resource;
    /**
     * The number of resources gained by this terrain
     */
    //private int bonus;
    //private int texture;
    private ArrayList<Texture> textures = new ArrayList<Texture>();
    private ArrayList<Model> models = new ArrayList<Model>();
    /**
     * The allowed improvements to build on this resource
     */
    private ArrayList<Improvement> improvements;

    public Terrain(String name, int moveCost, int waterCost, int score) { //, Resource resource, int bonus) {
        this.name = name;
        this.moveCost = moveCost;
        this.waterCost = waterCost;
        this.score = score;
//        this.resource = resource;
//        this.bonus = bonus;
        this.improvements = new ArrayList<Improvement>();
        this.bonuses = new ArrayList<ResourceAmount>();
        //this.texture = -1;
        //this.texture = Texture.loadTexture("tiles/" + name.toLowerCase() + ".png");
        popup = null;
        try {
            int count = 0;

            do {
                String mdlName = "tiles/" + name.toLowerCase() + (count == 0 ? "" : String.format("%02d", count)) + ".png";

//                URL url = Main.class.getResource(mdlName);
//                if(url == null) break;
                if (ResourceLoader.resourceExists(mdlName)) {

                    Texture t = new Texture(mdlName);
                    if (t == null) {
                        break;
                    }
                    this.textures.add(t);
                    count++;
                } else {
                    break;
                }
            } while (true);
        } catch (Exception ex) {
        }



        try {
            int count = 0;
            while (true) {
                String mdlName = "tiles/" + name.toLowerCase() + (count == 0 ? "" : String.format("%02d", count));
                //URL url = Main.class.getResource(mdlName + ".obj");
//                if (url == null) {
//                    break;
//                }
                if (ResourceLoader.resourceExists(mdlName + ".obj")) {
                    Model m = ModelLoader.loadModel(mdlName + ".obj");

                    m.normalise();
                    m.setTexture(new Texture(mdlName + ".png"));
                    this.models.add(m);
                    count++;
                } else {
                    break;
                }
            }
        } catch (Exception ex) {
        }
    }

    public JPanel getPopup() {
        if (popup == null) {
            popup = new ResourcesPanel();
            for (ResourceAmount ra : bonuses) {
                popup.addResource(ra);
            }
            popup.calculate();
        }
        return popup;
    }
    public int model_count = -1;

    public int getNextModelIndex() {
        if (getModelCount() == 0) {
            return -1;
        }
        model_count = (model_count + 1) % getModelCount();
        //if(getModelCount() > 1) System.out.println(getModelCount());
        return model_count;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public Model getModel() {
        if (models.isEmpty()) {
            return null;
        }
        return models.get(0);
    }

    public Model getModel(int index) {
        if (index < 0 || index >= models.size()) {
            return getModel();
        }
        return models.get(index);
    }

    public int getModelCount() {
        return models.size();
    }

    public int getWaterCost() {
        return waterCost;
    }

    public void setWaterCost(int waterCost) {
        this.waterCost = waterCost;
    }

    public int getMoveCost() {
        return moveCost;
    }

    public void setMoveCost(int moveCost) {
        this.moveCost = moveCost;
    }
    public int texture_count = 0;

    public int getNextTextureIndex() {
        if (getTextureCount() == 0) {
            return -1;
        }
        texture_count = (texture_count + 1) % getTextureCount();
        return texture_count;
    }

    public Texture getTexture() {
        if (textures.isEmpty()) {
            return null;
        }
        return textures.get(0);
    }

    public Texture getTexture(int index) {
        if (index < 0 || index >= textures.size()) {
            return getTexture();
        }
        return textures.get(index);
    }

    public int getTextureCount() {
        return textures.size();
    }

//    public int getTexture() {
//        return texture;
//    }
//    public void setTexture(int texture) {
//        this.texture = texture;
//    }
//    public int getBonus() {
//        return bonus;
//    }
//
//    public void setBonus(int bonus) {
//        this.bonus = bonus;
//    }
    public ArrayList<Improvement> getImprovements() {
        return improvements;
    }

    public void addImprovement(Improvement improvement) {
        this.improvements.add(improvement);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

//    public Resource getResource() {
//        return resource;
//    }
//
//    public void setResource(Resource resource) {
//        this.resource = resource;
//    }
    public void addBonus(Resource r, int a) {
        this.bonuses.add(new ResourceAmount(r, a));
    }

    public ArrayList<ResourceAmount> getBonuses() {
        return bonuses;
    }
}
